home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / h / dlgauto.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  9.8 KB  |  471 lines  |  [TEXT/MPS ]

  1. /* dlgauto.h automaton
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.23
  24.  * Will Cohen and Terence Parr
  25.  * Parr Research Corporation
  26.  * with Purdue University and AHPCRC, University of Minnesota
  27.  * 1989-1994
  28.  */
  29.  
  30. #ifndef ZZDEFAUTO_H
  31. #define ZZDEFAUTO_H
  32.  
  33. zzchar_t    *zzlextext;    /* text of most recently matched token */
  34. zzchar_t    *zzbegexpr;    /* beginning of last reg expr recogn. */
  35. zzchar_t    *zzendexpr;    /* beginning of last reg expr recogn. */
  36. int    zzbufsize;    /* number of characters in zzlextext */
  37. int    zzbegcol = 0;    /* column that first character of token is in*/
  38. int    zzendcol = 0;    /* column that last character of token is in */
  39. int    zzline = 1;    /* line current token is on */
  40. int    zzreal_line=1;    /* line of 1st portion of token that is not skipped */
  41. int    zzchar;        /* character to determine next state */
  42. int    zzbufovf;    /* indicates that buffer too small for text */
  43. int    zzcharfull = 0;
  44. static zzchar_t    *zznextpos;/* points to next available position in zzlextext*/
  45. static int     zzclass;
  46.  
  47. #ifdef __STDC__
  48. void    zzerrstd(char *);
  49. void    (*zzerr)(char *)=zzerrstd;/* pointer to error reporting function */
  50. extern int    zzerr_in(void);
  51. #else
  52. void    zzerrstd();
  53. void    (*zzerr)()=zzerrstd;    /* pointer to error reporting function */
  54. extern int    zzerr_in();
  55. #endif
  56.  
  57. static FILE    *zzstream_in=0;
  58. static int    (*zzfunc_in)() = zzerr_in;
  59. static zzchar_t    *zzstr_in=0;
  60.  
  61. static int     zzauto = 0;
  62. static int    zzadd_erase;
  63. static char     zzebuf[70];
  64.  
  65. #ifdef ZZCOL
  66. #define ZZINC (++zzendcol)
  67. #else
  68. #define ZZINC
  69. #endif
  70.  
  71.  
  72. #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
  73. #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
  74. #define ZZGETC_STR {             \
  75.     if (*zzstr_in){                \
  76.         zzchar = *zzstr_in;        \
  77.         ++zzstr_in;                \
  78.     }else{                        \
  79.         zzchar = EOF;            \
  80.     }                            \
  81.     zzclass = ZZSHIFT(zzchar);    \
  82. }
  83.  
  84. #define ZZNEWSTATE    (newstate = dfa[state][zzclass])
  85.  
  86. #ifndef ZZCOPY
  87. #define ZZCOPY    \
  88.     /* Truncate matching buffer to size (not an error) */    \
  89.     if (zznextpos < lastpos){                \
  90.         *(zznextpos++) = zzchar;            \
  91.     }else{                            \
  92.         zzbufovf = 1;                    \
  93.     }
  94. #endif
  95.  
  96. void
  97. #ifdef __STDC__
  98. zzrdstream( FILE *f )
  99. #else
  100. zzrdstream( f )
  101. FILE *f;
  102. #endif
  103. {
  104.     /* make sure that it is really set to something, otherwise just 
  105.        leave it be.
  106.     */
  107.     if (f){
  108.         /* make sure that there is always someplace to get input
  109.            before closing zzstream_in
  110.         */
  111. #if 0
  112.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  113. #endif
  114.         zzline = 1;
  115.         zzstream_in = f;
  116.         zzfunc_in = NULL;
  117.         zzstr_in = 0;
  118.         zzcharfull = 0;
  119.     }
  120. }
  121.  
  122. void
  123. #ifdef __STDC__
  124. zzrdfunc( int (*f)() )
  125. #else
  126. zzrdfunc( f )
  127. int (*f)();
  128. #endif
  129. {
  130.     /* make sure that it is really set to something, otherwise just 
  131.        leave it be.
  132.     */
  133.     if (f){
  134.         /* make sure that there is always someplace to get input
  135.            before closing zzstream_in
  136.         */
  137. #if 0
  138.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  139. #endif
  140.         zzline = 1;
  141.         zzstream_in = NULL;
  142.         zzfunc_in = f;
  143.         zzstr_in = 0;
  144.         zzcharfull = 0;
  145.     }
  146. }
  147.  
  148.  
  149. void
  150. #ifdef __STDC__
  151. zzrdstr( zzchar_t *s )
  152. #else
  153. zzrdstr( s )
  154. zzchar_t *s;
  155. #endif
  156. {
  157.     /* make sure that it is really set to something, otherwise just 
  158.        leave it be.
  159.     */
  160.     if (s){
  161.         /* make sure that there is always someplace to get input
  162.            before closing zzstream_in
  163.         */
  164. #if 0
  165.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  166. #endif
  167.         zzline = 1;
  168.         zzstream_in = NULL;
  169.         zzfunc_in = 0;
  170.         zzstr_in = s;
  171.         zzcharfull = 0;
  172.     }
  173. }
  174.  
  175.  
  176. void
  177. zzclose_stream()
  178. {
  179. #if 0
  180.     fclose( zzstream_in );
  181.     zzstream_in = NULL;
  182.     zzfunc_in = NULL;
  183. #endif
  184. }
  185.  
  186. /* saves dlg state, but not what feeds dlg (such as file position) */
  187. void
  188. #ifdef __STDC__
  189. zzsave_dlg_state(struct zzdlg_state *state)
  190. #else
  191. zzsave_dlg_state(state)
  192. struct zzdlg_state *state;
  193. #endif
  194. {
  195.     state->stream = zzstream_in;
  196.     state->func_ptr = zzfunc_in;
  197.     state->str = zzstr_in;
  198.     state->auto_num = zzauto;
  199.     state->add_erase = zzadd_erase;
  200.     state->lookc = zzchar;
  201.     state->char_full = zzcharfull;
  202.     state->begcol = zzbegcol;
  203.     state->endcol = zzendcol;
  204.     state->line = zzline;
  205.     state->lextext = zzlextext;
  206.     state->begexpr = zzbegexpr;
  207.     state->endexpr = zzendexpr;
  208.     state->bufsize = zzbufsize;
  209.     state->bufovf = zzbufovf;
  210.     state->nextpos = zznextpos;
  211.     state->class_num = zzclass;
  212. }
  213.  
  214. void
  215. #ifdef __STDC__
  216. zzrestore_dlg_state(struct zzdlg_state *state)
  217. #else
  218. zzrestore_dlg_state(state)
  219. struct zzdlg_state *state;
  220. #endif
  221. {
  222.     zzstream_in = state->stream;
  223.     zzfunc_in = state->func_ptr;
  224.     zzstr_in = state->str;
  225.     zzauto = state->auto_num;
  226.     zzadd_erase = state->add_erase;
  227.     zzchar = state->lookc;
  228.     zzcharfull = state->char_full;
  229.     zzbegcol = state->begcol;
  230.     zzendcol = state->endcol;
  231.     zzline = state->line;
  232.     zzlextext = state->lextext;
  233.     zzbegexpr = state->begexpr;
  234.     zzendexpr = state->endexpr;
  235.     zzbufsize = state->bufsize;
  236.     zzbufovf = state->bufovf;
  237.     zznextpos = state->nextpos;
  238.     zzclass = state->class_num;
  239. }
  240.  
  241. void
  242. #ifdef __STDC__
  243. zzmode( int m )
  244. #else
  245. zzmode( m )
  246. int m;
  247. #endif
  248. {
  249.     /* points to base of dfa table */
  250.     if (m<MAX_MODE){
  251.         zzauto = m;
  252.         /* have to redo class since using different compression */
  253.         zzclass = ZZSHIFT(zzchar);
  254.     }else{
  255.         sprintf(zzebuf,"Invalid automaton mode = %d ",m);
  256.         zzerr(zzebuf);
  257.     }
  258. }
  259.  
  260. /* erase what is currently in the buffer, and get a new reg. expr */
  261. void
  262. zzskip()
  263. {
  264.     zzadd_erase = 1;
  265. }
  266.  
  267. /* don't erase what is in the zzlextext buffer, add on to it */
  268. void
  269. zzmore()
  270. {
  271.     zzadd_erase = 2;
  272. }
  273.  
  274. /* substitute c for the reg. expr last matched and is in the buffer */
  275. #ifdef __STDC__
  276. void
  277. zzreplchar(zzchar_t c)
  278. #else
  279. void
  280. zzreplchar(c)
  281. zzchar_t c;
  282. #endif
  283. {
  284.     /* can't allow overwriting null at end of string */
  285.     if (zzbegexpr < &zzlextext[zzbufsize-1]){
  286.         *zzbegexpr = c;
  287.         *(zzbegexpr+1) = '\0';
  288.     }
  289.     zzendexpr = zzbegexpr;
  290.     zznextpos = zzbegexpr + 1;
  291. }
  292.  
  293. /* replace the string s for the reg. expr last matched and in the buffer */
  294. void
  295. #ifdef __STDC__
  296. zzreplstr(register zzchar_t *s)
  297. #else
  298. zzreplstr(s)
  299. register zzchar_t *s;
  300. #endif
  301. {
  302.     register zzchar_t *l= &zzlextext[zzbufsize -1];
  303.  
  304.     zznextpos = zzbegexpr;
  305.     if (s){
  306.          while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
  307.             /* empty */
  308.         }
  309.         /* correct for NULL at end of string */
  310.         zznextpos--;
  311.     }
  312.     if ((zznextpos <= l) && (*(--s) == 0)){
  313.         zzbufovf = 0;
  314.     }else{
  315.         zzbufovf = 1;
  316.     }
  317.     *(zznextpos) = '\0';
  318.     zzendexpr = zznextpos - 1;
  319. }
  320.  
  321. void
  322. zzgettok()
  323. {
  324.     register int state, newstate;
  325.     /* last space reserved for the null char */
  326.     register zzchar_t *lastpos;
  327.  
  328. skip:
  329.     zzreal_line = zzline;
  330.     zzbufovf = 0;
  331.     lastpos = &zzlextext[zzbufsize-1];
  332.     zznextpos = zzlextext;
  333.     zzbegcol = zzendcol+1;
  334. more:
  335.     zzbegexpr = zznextpos;
  336. #ifdef ZZINTERACTIVE
  337.     /* interactive version of automaton */
  338.     /* if there is something in zzchar, process it */
  339.     state = newstate = dfa_base[zzauto];
  340.     if (zzcharfull){
  341.         ZZINC;
  342.         ZZCOPY;
  343.         ZZNEWSTATE;
  344.     }
  345.     if (zzstr_in)
  346.         while (zzalternatives[newstate]){
  347.             state = newstate;
  348.             ZZGETC_STR;
  349.             ZZINC;
  350.             ZZCOPY;
  351.             ZZNEWSTATE;
  352.         }
  353.     else if (zzstream_in)
  354.         while (zzalternatives[newstate]){
  355.             state = newstate;
  356.             ZZGETC_STREAM;
  357.             ZZINC;
  358.             ZZCOPY;
  359.             ZZNEWSTATE;
  360.         }
  361.     else if (zzfunc_in)
  362.         while (zzalternatives[newstate]){
  363.             state = newstate;
  364.             ZZGETC_FUNC;
  365.             ZZINC;
  366.             ZZCOPY;
  367.             ZZNEWSTATE;
  368.         }
  369.     /* figure out if last character really part of token */
  370.     if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
  371.         zzcharfull = 1;
  372.         --zznextpos;
  373.     }else{
  374.         zzcharfull = 0;
  375.         state = newstate;
  376.     }
  377.     *(zznextpos) = '\0';
  378.     /* Able to transition out of start state to some non err state?*/
  379.     if ( state == dfa_base[zzauto] ){
  380.         /* make sure doesn't get stuck */
  381.         zzadvance();
  382.     }
  383. #else
  384.     /* non-interactive version of automaton */
  385.     if (!zzcharfull)
  386.         zzadvance();
  387.     else
  388.         ZZINC;
  389.     state = dfa_base[zzauto];
  390.     if (zzstr_in)
  391.         while (ZZNEWSTATE != DfaStates){
  392.             state = newstate;
  393.             ZZCOPY;
  394.             ZZGETC_STR;
  395.             ZZINC;
  396.         }
  397.     else if (zzstream_in)
  398.         while (ZZNEWSTATE != DfaStates){
  399.             state = newstate;
  400.             ZZCOPY;
  401.             ZZGETC_STREAM;
  402.             ZZINC;
  403.         }
  404.     else if (zzfunc_in)
  405.         while (ZZNEWSTATE != DfaStates){
  406.             state = newstate;
  407.             ZZCOPY;
  408.             ZZGETC_FUNC;
  409.             ZZINC;
  410.         }
  411.     zzcharfull = 1;
  412.     if ( state == dfa_base[zzauto] ){
  413.         if (zznextpos < lastpos){
  414.             *(zznextpos++) = zzchar;
  415.         }else{
  416.             zzbufovf = 1;
  417.         }
  418.         *zznextpos = '\0';
  419.         /* make sure doesn't get stuck */
  420.         zzadvance();
  421.     }else{
  422.         *zznextpos = '\0';
  423.     }
  424. #endif
  425. #ifdef ZZCOL
  426.     zzendcol -= zzcharfull;
  427. #endif
  428.     zzendexpr = zznextpos -1;
  429.     zzadd_erase = 0;
  430.     (*actions[accepts[state]])();
  431.     switch (zzadd_erase) {
  432.         case 1: goto skip;
  433.         case 2: goto more;
  434.     }
  435. }
  436.  
  437. void
  438. zzadvance()
  439. {
  440.     if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
  441.     if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
  442.     if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
  443.     if (!(zzstream_in || zzfunc_in || zzstr_in)){
  444.         zzerr_in();
  445.     }
  446. }
  447.  
  448. void
  449. #ifdef __STDC__
  450. zzerrstd(char *s)
  451. #else
  452. zzerrstd(s)
  453. char *s;
  454. #endif
  455. {
  456.         fprintf(stderr,
  457.                 "%s near line %d (text was '%s')\n",
  458.                 ((s == NULL) ? "Lexical error" : s),
  459.                 zzline,zzlextext);
  460. }
  461.  
  462. int
  463. zzerr_in()
  464. {
  465.     fprintf(stderr,"No input stream, function, or string\n");
  466.     /* return eof to get out gracefully */
  467.     return EOF;
  468. }
  469.  
  470. #endif
  471.